package cz.muni.fi.pv168.confusinglibrary.app.gui;

import cz.muni.fi.pv168.confusinglibrary.libs.Author;
import java.util.ArrayList;
import java.util.List;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author Riema
 */
public class AuthorTableModel extends AbstractTableModel {

    private List<Author> allAuthors = new ArrayList<Author>();
    
    public void fillTable(List<Author> authors){
        allAuthors = new ArrayList<Author>(authors);
        fireTableRowsInserted(0, authors.size()-1);
    }
    
    public void addAuthor(Author author) {
        allAuthors.add(author);
        int lastRow = allAuthors.size() - 1;
        fireTableRowsInserted(lastRow, lastRow);
    }
    
    public void updateAuthor(Author author, int row){
        allAuthors.set(row, author);
        fireTableRowsUpdated(row, row);
    }

    public void removeAuthor(int row){
        allAuthors.remove(row);
        fireTableRowsDeleted(row, row);
    }
            
    public int getRowCount() {
        return allAuthors.size();
    }

    public int getColumnCount() {
        return 2;
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        Author author = allAuthors.get(rowIndex);
        switch (columnIndex) {
            case 0:
                return author.getSurname();
            case 1:
                return author.getName();
            case 2:
                return author.getId();
            default:
                throw new IllegalArgumentException("columnIndex");
        }
    }

    @Override
    public String getColumnName(int columnIndex) {
        switch (columnIndex) {
            case 0:
                return java.util.ResourceBundle.getBundle("cz/muni/fi/pv168/confusinglibrary/app/language/language").getString("table_surname");
            case 1:
                return java.util.ResourceBundle.getBundle("cz/muni/fi/pv168/confusinglibrary/app/language/language").getString("table_name");
            case 2:
                return java.util.ResourceBundle.getBundle("cz/muni/fi/pv168/confusinglibrary/app/language/language").getString("table_id");
            default:
                throw new IllegalArgumentException("columnIndex");
        }
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        switch (columnIndex) {
            case 0:
                return String.class;
            case 1:
                return String.class;
            case 2:
                return Long.class;
            default:
                throw new IllegalArgumentException("columnIndex");
        }
    }

    protected Long getId(int selectedRow) {
        return allAuthors.get(selectedRow).getId();
    }
}
